package com.compass.doctor.ui.chat.viewholder;

import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.compass.doctor.R;
import com.compass.doctor.databinding.ChatMessagePrescriptionViewHolderBinding;
import com.compass.doctor.ui.WebDetailActivity;
import com.compass.doctor.ui.chat.ImDrugAdapter;
import com.compass.doctor.ui.chat.attachment.InquiryAttachment;
import com.compass.doctor.ui.chat.attachment.PrescriptionAttachment;
import com.compass.framework.model.ImDrug;
import com.compass.framework.utils.BigDecimalUtils;
import com.compass.framework.utils.LogUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.netease.yunxin.kit.chatkit.ui.common.MessageHelper;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding;
import com.netease.yunxin.kit.chatkit.ui.fun.view.message.viewholder.FunChatBaseMessageViewHolder;
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean;

import java.lang.reflect.Type;
import java.util.ArrayList;

/**
 * Description： 处方单ViewHolder
 * Date:2024/1/19 10:19
 */
public class PrescriptionViewHolder extends FunChatBaseMessageViewHolder {
    public static final String TAG = "InquiryViewHolder";
    ChatMessagePrescriptionViewHolderBinding binding;
    PrescriptionAttachment attachment;
    ImDrugAdapter drugAdapter;
    ArrayList<ImDrug> imDrugs = new ArrayList<>();

    public PrescriptionViewHolder(@NonNull ChatBaseMessageViewHolderBinding parent, int viewType) {
        super(parent, viewType);
    }

    @Override
    protected void addViewToMessageContainer() {
        binding = ChatMessagePrescriptionViewHolderBinding.inflate(LayoutInflater.from(parent.getContext()), getMessageContainer(), true);

    }


    @Override
    public void bindData(ChatMessageBean message, ChatMessageBean lastMessage) {
        super.bindData(message, lastMessage);
        currentMessage = message;
        attachment = (PrescriptionAttachment) message.getMessageData().getMessage().getAttachment();
        if (attachment == null) {
            return;
        }
        //设置ViewHolder展示UI的内容
        //解析药品内容
        Gson gson=new Gson();
        Type type=new TypeToken<ArrayList<ImDrug>>(){}.getType();
        imDrugs=gson.fromJson(attachment.getDrugs(),type);

        binding.rcyDrug.setLayoutManager(new LinearLayoutManager(parent.getContext()));
        drugAdapter = new ImDrugAdapter();
        binding.rcyDrug.setAdapter(drugAdapter);
        drugAdapter.setData(imDrugs);

        binding.describe.setText("诊断："+attachment.getDescribe());
        binding.tvPrice.setText("￥"+ BigDecimalUtils.divide(attachment.getPrice()));
        if (itemClickListener != null) {
            binding.tvDetails.setOnClickListener(
                    v -> itemClickListener.onMessageClick(v, position, currentMessage));
        }



    }


    @Override
    protected void onMessageBackgroundConfig(ChatMessageBean messageBean) {
        super.onMessageBackgroundConfig(messageBean);
        boolean isReceivedMsg = MessageHelper.isReceivedMessage(messageBean);
        View firstChild = baseViewBinding.messageContainer.getChildAt(0);
        if (isReceivedMsg) {
            firstChild.setBackgroundResource(R.drawable.bg_custom_message);
        } else {
            firstChild.setBackgroundResource(R.drawable.bg_custom_message_out);
        }
    }
}
